import html2canvas from 'html2canvas'

// 下载到本地
// const CaptureImg = async (element: HTMLElement) => {
//   try {
//     const canvas = await html2canvas(element)
//     const image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream')
//     const link = document.createElement('a')
//     link.setAttribute('href', image)
//     link.setAttribute('download', '截图盒子.png')
//     link.click()
//   } catch (error) {
//     console.error('An error occurred while capturing the image:', error)
//   }
// }

// -------------------------------------------------------------------------------------

const CaptureImg = async (element: HTMLElement) => {
  try {
    const canvas = await html2canvas(element)

    // 生成 File文件 ----------------------------------------------
    const blob = await new Promise<Blob>(resolve => {
      canvas.toBlob(b => {
        resolve(b as Blob)
      })
    })
    const file = new File([blob], 'screenshot.png', { type: 'image/png' })
    console.log('file=', file)

    // 保存为本地图片 ----------------------------------------------
    const image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream')
    const link = document.createElement('a')
    link.setAttribute('href', image)
    link.setAttribute('download', '截图盒子.png')
    link.click()
  } catch (error) {
    console.error('An error occurred while capturing the image:', error)
    throw error
  }
}

export default CaptureImg
